package com.xbongbong.crm.perform.query.list.button;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.domain.entity.CustomerFocusEntity;
import com.xbongbong.saas.enums.CompanyConfigEnum;
import com.xbongbong.saas.enums.ListOptionEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.CustomerFocusModel;
import com.xbongbong.sys.model.CompanyConfigModel;
import org.elasticsearch.common.inject.Singleton;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * 客户的关注
 * @author 吴峰
 * @date 2021/12/20 14:47
 */
@Component
public class CustomerFocus {

    @Resource
    private CompanyConfigModel companyConfigModel;
    @Resource
    private CustomerFocusModel customerFocusModel;

    private boolean enable;

    private Collection<Long> list;



    public CustomerFocus() {
    }

    public CustomerFocus(boolean enable, Collection<Long> list) {
        this.enable = enable;
        this.list = list;
    }

    /**
     * 获取关注列表
     * @param corpid
     * @param userId
     * @param dataIdList
     * @return
     * @throws XbbException
     */
    private CustomerFocus getFocusList( String corpid, String userId, List<Long> dataIdList , Integer businessType, Integer subBusinessType) throws XbbException {
        List<Integer> customerSubList = Arrays.asList(XbbRefTypeEnum.CUSTOMER_RECYCLE.getCode(), XbbRefTypeEnum.CUSTOMER_PUBLIC.getCode());
        if (!customerSubList.contains(subBusinessType)) {
            boolean isCusShare = companyConfigModel.hasBeenEnable(corpid, CompanyConfigEnum.SHARE_FOCUS_CUSTOEMR.getAlias(), BasicConstant.IS_USE.toString());
            Set<Long> list = new HashSet<>();
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            map.put("corpid", corpid);
            if(!isCusShare) {
                map.put("userId", userId);
                map.put("groupByStr", " customer_id ");
            }
            map.put("customerIdIn", dataIdList);
            map.put("del", 0);
            map.put("focus", 1);
            List<CustomerFocusEntity> customerFocusEntityList = customerFocusModel.findEntitys(map);
            for (CustomerFocusEntity entity : customerFocusEntityList) {
                list.add(entity.getCustomerId());
            }

            return new CustomerFocus(true, list);
        } else {
            return new CustomerFocus(false, new ArrayList<>());
        }
    }


    public CustomerFocus setFocusField( String corpid, String userId, List<Long> dataIdList, List<FieldAttrEntity> list, Integer businessType, Integer subBusinessType) throws XbbException {
        CustomerFocus focus = getFocusList(corpid, userId, dataIdList, businessType, subBusinessType);
        if (focus.isEnable()) {
            FieldAttrEntity fieldFocus = new FieldAttrEntity();
            fieldFocus.setAttr(ListOptionEnum.FOCUS.getAttr());
            fieldFocus.setAttrName(ListOptionEnum.FOCUS.getAttrName());
            fieldFocus.setFieldType(FieldTypeEnum.FOCUS.getType());
            fieldFocus.setDisableListSort(1);
            list.add(0, fieldFocus);
        }
        return focus;
    }

    public static void setFocusData(CustomerFocus focus, PaasFormDataEntityExt paasFormDataEntityExt) {
        if (focus.isEnable()) {
            JSONObject json = new JSONObject();
            Long dataId = paasFormDataEntityExt.getId();
            JSONObject data = paasFormDataEntityExt.getData();
            json.put("value", focus.getList().contains(dataId) ? 1 : 0);
            data.put(ListOptionEnum.FOCUS.getAttr(), json);
        }
    }

    public boolean isEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    public Collection<Long> getList() {
        return list;
    }

    public void setList(Collection<Long> list) {
        this.list = list;
    }
}
